﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class EnemySpawner : MonoBehaviour {

	public List<GameObject> Enemies;
	public List<GameObject> Spawners;
	public float enemyTimer;
	public float spawntime = 5;
	public bool Spawning = false;

	// Use this for initialization
	void Start () 
	{
		MainService.GameStart += BeginSpawning;
		MainService.GameEnd += StopSpawning;
	}
	
	void OnDisable()
	{
		MainService.GameStart -= BeginSpawning;
		MainService.GameEnd -= StopSpawning;
	}
	
	void StopSpawning()
	{
		Spawning = false;
	}
	
	void BeginSpawning()
	{
		Spawning = true;
	}
	
	// Update is called once per frame
	void Update () 
	{
		if (Spawning == true)
		{
			enemyTimer += 1.0F * Time.deltaTime;
			if (enemyTimer > spawntime)
			{
				Instantiate (Enemies[Random.Range (0,Enemies.Count)] ,Spawners[Random.Range(0,Spawners.Count)].transform.position, Quaternion.identity);
				enemyTimer = 0;
			}
		}

	}
}
